#include <common.h>
#include <idt.h>
#include <thread.h>

unsigned int tick = 0;
unsigned int tick2 = 0;

registers *timer_tick(registers *r)
{
	tick++;
	timer_ticks++;
	//printf("\n%x",timer_ticks);
	outb(0x20, 0x20);
	if(current_thread)
	{
		current_thread->time++;
		if(current_thread->time >5)
		{	
			current_thread->time = 0;
			schedule(r);
		}
	}

	return r;
}


void init_timer(unsigned int freq)
{
	register_int_handler(IRQ0, &timer_tick);
	
	unsigned int divisor = 1193180/freq;
	outb(0x43, 0x36);
	outb(0x40,divisor & 0xFF);
	outb(0x40,(divisor>>8)&0xFF);
}

